from django.db import models
from category_group.models import Category


class Brand(models.Model):
    brand_name = models.CharField(max_length=30)
    first_letter = models.CharField(max_length=30)
    logo_addr = models.CharField(max_length=300)

    class Meta:
        db_table = 'brand'

    def __str__(self):
        return self.brand_name


class SPU(models.Model):
    "商品SPU，如小米手机10；苹果13"
    name = models.CharField(max_length=20, verbose_name="spu")
    brand = models.ForeignKey(Brand, on_delete=models.PROTECT, verbose_name='品牌')
    category1 = models.ForeignKey(Category, on_delete=models.PROTECT, related_name='cate1_spu', verbose_name='一级类别')
    category2 = models.ForeignKey(Category, on_delete=models.PROTECT, related_name='cate2_spu', verbose_name='二级类别')
    category3 = models.ForeignKey(Category, on_delete=models.PROTECT, related_name='cate3_spu', verbose_name='三级类别')
    comment_num = models.IntegerField(verbose_name="评论量",default=0)
    sales = models.IntegerField(verbose_name="销量",default=0)
    desc_detail = models.TextField(default='', verbose_name='详细介绍')
    desc_pack = models.TextField(default='', verbose_name='包装信息')
    desc_service = models.TextField(default='', verbose_name='售后服务')

    class Meta:
        db_table = 'spu'

    def __str__(self):
        return self.name


class SKU(models.Model):
    """
    商品表
        名称、描述、价格、库存、商品详情、商品默认图片、分类
    """
    name = models.CharField(max_length=100)
    describe = models.CharField(max_length=500)
    price = models.DecimalField(max_digits=8, decimal_places=2)
    stock = models.IntegerField(default=0)
    detail = models.TextField()
    image_default = models.CharField(verbose_name="默认图片的地址", max_length=300)

    is_selling = models.BooleanField(default=True)
    sales = models.IntegerField(verbose_name="销量", default=0)
    # 一级分类
    cate = models.ForeignKey(Category, on_delete=models.CASCADE)
    spu = models.ForeignKey(SPU, on_delete=models.CASCADE, verbose_name='商品SPU')

    class Meta:
        db_table = 'sku'

    def __str__(self):
        return self.name


class SPUSpecification(models.Model):
    """商品SPU规格"""
    spu = models.ForeignKey(SPU, on_delete=models.CASCADE, related_name='specs', verbose_name='商品SPU')
    name = models.CharField(max_length=20, verbose_name='规格名称')

    class Meta:
        db_table = 'spu_spec'
        verbose_name_plural = '商品SPU规格'

    def __str__(self):
        return '%s: %s' % (self.spu.name, self.name)


class SpecificationOption(models.Model):
    """SPU规格选项"""
    spec = models.ForeignKey(SPUSpecification, related_name='options', on_delete=models.CASCADE, verbose_name='规格')
    value = models.CharField(max_length=20, verbose_name='选项值')

    class Meta:
        db_table = 'spec_option'
        verbose_name_plural = '规格选项'

    def __str__(self):
        return '%s - %s' % (self.spec, self.value)


class SKUImage(models.Model):
    """SKU图片"""
    sku = models.ForeignKey(to=SKU, on_delete=models.CASCADE, verbose_name='sku')
    image = models.CharField(verbose_name='商品图片', max_length=300)

    class Meta:
        db_table = 'sku_image'
        verbose_name_plural = 'SKU图片'

    def __str__(self):
        return '%s %s' % (self.sku.name, self.id)












